package buzzword.gui;

import java.util.List;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import json.JSONException;

import buzzword.Board;
import buzzword.Field;
import buzzword.GameEndedException;
import buzzword.GameNotStartetException;
import buzzword.NoGameFoundException;
import buzzword.ServerCommunicationException;

public class GameBoardPanel extends JPanel{

	private JButton[][] buttonsOnBoard = null;
	private GameControlPanel controlPanel = null;
	//x achse sind columns und y achse sind rows
	public GameBoardPanel() {
		//set size
		int[] size = GuiMainFrame.getClient().getBoardSizes();
		this.setOpaque(false);
		this.setSize((size[1]*100)+120, size[0]*100);
		buttonsOnBoard = new JButton[size[1]][size[0]];
		this.setLayout(null);
		
		for(int x = 0; x < buttonsOnBoard.length;x++) {
			for(int y = 0; y < buttonsOnBoard[x].length;y++) {
				buttonsOnBoard[x][y] = new JButton();
				buttonsOnBoard[x][y].setBounds(x*100,y*100,100,100);
				this.add(buttonsOnBoard[x][y]);
				buttonsOnBoard[x][y].addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						((JButton) e.getSource()).setEnabled(false);
					}
				});
			}
		}
		//paint the controlPanel
		controlPanel = new GameControlPanel(this.getHeight());
		controlPanel.setLocation(this.getWidth()-100, 0);
		add(controlPanel);
		//now fill the buttons with words
		try {
			List<Field> boardFields = GuiMainFrame.getClient().getBoardFields();
			for(Field currentField : boardFields) {
				buttonsOnBoard[currentField.getColumn()-1][currentField.getRow()-1].setText(currentField.value());
			}
		} catch (NoGameFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (GameNotStartetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServerCommunicationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (GameEndedException e) {
			
		}
	}
}
